<template>
  <a-modal
    :title="$t('批量修改')"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okText="$t('保存')"
    :cancelText="$t('关闭')"
    @ok="handleOk"
    @cancel="handleCancel"
    v-drag
  >
    <a-spin :spinning="confirmLoading">
      <a-form class="batchForm">
        <a-form-item :label="$t('选择修改的属性')" :labelCol="{ span: 7 }" :wrapperCol="{ span: 15 }">
          <a-select
            v-model="dataForm.editState"
            allowClear
            @change="changeType"
            :getPopupContainer="(triggerNode) => { return triggerNode.parentNode || document.body }"
          >
            <a-select-option key="state">{{ $t('上架状态') }}</a-select-option>
            <a-select-option key="allowMemberDiscount">{{ $t('是否允许享受会员优惠') }}</a-select-option>
            <a-select-option key="kind">{{ $t('商品分类') }}</a-select-option>
            <a-select-option key="allowSale">{{ $t('销售规则') }}</a-select-option>
            <a-select-option key="allowCoin">{{ $t('币兑换规则') }}</a-select-option>
            <a-select-option key="allowLottery">{{ $t('票兑换规则') }}</a-select-option>
            <a-select-option key="allowIntegral">{{ $t('积分兑换规则') }}</a-select-option>
            <a-select-option key="allowPoints">{{ $t('A币兑换规则') }}</a-select-option>
            <a-select-option key="allowAlottery">{{ $t('A票兑换规则') }}</a-select-option>
            <a-select-option key="barCode">{{ $t('生成条形码') }}</a-select-option>
            <a-select-option key="cost">{{ $t('参考成本') }}</a-select-option>
            <a-select-option key="recyclingList">{{ $t('回收规则') }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          v-if="
            dataForm.editState !== 'kind' &&
            dataForm.editState !== 'barCode' &&
            dataForm.editState !== 'price' &&
            dataForm.editState !== 'cost' &&
            dataForm.editState !== 'recyclingList' &&
            dataForm.editState !== 'allowSale' &&
            dataForm.editState !== 'allowCoin' &&
            dataForm.editState !== 'allowLottery' &&
            dataForm.editState !== 'allowIntegral' &&
            dataForm.editState !== 'allowPoints' &&
            dataForm.editState !== 'allowAlottery'
          "
          :label="$t('修改属性为')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-select v-model="dataForm.saveState" allowClear>
            <a-select-option key="Y">{{ $t('是') }}</a-select-option>
            <a-select-option key="N">{{ $t('否') }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'kind'"
          :label="$t('修改属性为')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-tree-select
            allowClear
            v-model="dataForm.saveState"
            :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
            :tree-data="treeData"
            :search-placeholder="$t('请选择')"
            :getPopupContainer="(triggerNode) => { return triggerNode.parentNode || document.body }"
          />
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'barCode'"
          :label="$t('已选商品数量')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          {{ goodsIdList.length }}
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'cost'"
          :label="$t('改价方案')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-radio-group v-model="eType">
            <a-radio value="1">{{ $t('指定价格') }}</a-radio>
            <a-radio value="2">{{ $t('按比例') }}</a-radio>
          </a-radio-group>
          <div v-if="eType == '1'">
            {{ $t('改后成本') }} = <a-input v-model="dataForm.cost" style="width: 190px"></a-input>
          </div>
          <div v-if="eType == '2'">
            {{ $t('改后成本') }} = {{ $t('现有成本') }} + {{ $t('现有成本') }} ×
            <a-input v-model="dataForm.rate" style="width: 90px"></a-input>%
          </div>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'recyclingList'"
          :label="$t('回收规则')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-checkbox
            style="color: black; font-weight: 500; font-size: 15px"
            :checked="dataForm.status == 'Y'"
            @change="changeStatus"
            >{{ $t('允许回收') }}</a-checkbox
          >
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'recyclingList'"
          :label="$t('回收类型')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-select :placeholder="$t('请选择')" v-model="dataForm.recyclingType" :disabled="dataForm.status == 'N'">
            <a-select-option v-for="type in paytypeList" :key="type.name" :value="type.name">
              <span>{{ type.name_dictText }}</span>
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'recyclingList'"
          :label="$t('回收值')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-input v-model="dataForm.recyclingValue" :disabled="dataForm.status == 'N'" :min="1" :precision="0"></a-input>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowSale'"
          :label="$t('销售规则')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-checkbox
            style="color: black; font-weight: 500; font-size: 15px"
            :checked="dataForm[dataForm.editState] == 'Y'"
            @change="(e) => editStatus(e, dataForm.editState)"
            >{{ $t('允许销售') }}</a-checkbox
          >
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowSale'"
          :label="$t('改价方案')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-radio-group v-model="eType" :disabled="dataForm[dataForm.editState] == 'N'">
            <a-radio value="1">{{ $t('指定价格') }}</a-radio>
            <a-radio value="2">{{ $t('按比例') }}</a-radio>
          </a-radio-group>
          <div v-if="eType == '1'">
            {{ $t('售价') }} =
            <a-input v-model="dataForm.price" :disabled="dataForm.allowSale == 'N'" style="width: 190px"></a-input>
          </div>
          <div v-if="eType == '2'">
            {{ $t('售价') }} = {{ $t('现有售价') }} + {{ $t('现有售价') }} ×
            <a-input v-model="dataForm.rate" :disabled="dataForm.allowSale == 'N'" style="width: 90px"></a-input>%
          </div>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowCoin'"
          :label="$t('币兑换规则')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-checkbox
            style="color: black; font-weight: 500; font-size: 15px"
            :checked="dataForm[dataForm.editState] == 'Y'"
            @change="(e) => editStatus(e, dataForm.editState)"
            >{{ $t('允许币兑换') }}</a-checkbox
          >
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowCoin'"
          :label="$t('改价方案')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-radio-group v-model="eType" :disabled="dataForm[dataForm.editState] == 'N'">
            <a-radio value="1">{{ $t('指定价格') }}</a-radio>
            <a-radio value="2">{{ $t('按比例') }}</a-radio>
          </a-radio-group>
          <div v-if="eType == '1'">
            {{ $t('币兑换值') }} =
            <a-input v-model="dataForm.coin" :disabled="dataForm.allowCoin == 'N'" style="width: 190px"></a-input>
          </div>
          <div v-if="eType == '2'">
            {{ $t('币兑换值') }} = {{ $t('现有兑换值') }} + {{ $t('现有兑换值') }} ×
            <a-input v-model="dataForm.rate" :disabled="dataForm.allowCoin == 'N'" style="width: 90px"></a-input>%
          </div>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowLottery'"
          :label="$t('票兑换规则')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-checkbox
            style="color: black; font-weight: 500; font-size: 15px"
            :checked="dataForm[dataForm.editState] == 'Y'"
            @change="(e) => editStatus(e, dataForm.editState)"
            >{{ $t('允许彩票兑换') }}</a-checkbox
          >
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowLottery'"
          :label="$t('改价方案')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-radio-group v-model="eType" :disabled="dataForm[dataForm.editState] == 'N'">
            <a-radio value="1">{{ $t('指定价格') }}</a-radio>
            <a-radio value="2">{{ $t('按比例') }}</a-radio>
          </a-radio-group>
          <div v-if="eType == '1'">
            {{ $t('票兑换值') }} =
            <a-input v-model="dataForm.lottery" :disabled="dataForm.allowLottery == 'N'" style="width: 190px"></a-input>
          </div>
          <div v-if="eType == '2'">
            {{ $t('票兑换值') }} = {{ $t('现有兑换值') }} + {{ $t('现有兑换值') }} ×
            <a-input v-model="dataForm.rate" :disabled="dataForm.allowLottery == 'N'" style="width: 90px"></a-input>%
          </div>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowPoints'"
          :label="$t('A币兑换规则')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-checkbox
            style="color: black; font-weight: 500; font-size: 15px"
            :checked="dataForm[dataForm.editState] == 'Y'"
            @change="(e) => editStatus(e, dataForm.editState)"
            >{{ $t('允许A币兑换') }}</a-checkbox
          >
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowPoints'"
          :label="$t('改价方案')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-radio-group v-model="eType" :disabled="dataForm[dataForm.editState] == 'N'">
            <a-radio value="1">{{ $t('指定价格') }}</a-radio>
            <a-radio value="2">{{ $t('按比例') }}</a-radio>
          </a-radio-group>
          <div v-if="eType == '1'">
            {{ $t('A币兑换值') }} =
            <a-input v-model="dataForm.points" :disabled="dataForm.allowPoints == 'N'" style="width: 190px"></a-input>
          </div>
          <div v-if="eType == '2'">
            {{ $t('A币兑换值') }} = {{ $t('现有兑换值') }} + {{ $t('现有兑换值') }} ×
            <a-input v-model="dataForm.rate" :disabled="dataForm.allowPoints == 'N'" style="width: 90px"></a-input>%
          </div>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowAlottery'"
          :label="$t('A票兑换规则')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-checkbox
            style="color: black; font-weight: 500; font-size: 15px"
            :checked="dataForm[dataForm.editState] == 'Y'"
            @change="(e) => editStatus(e, dataForm.editState)"
            >{{ $t('允许A票兑换') }}</a-checkbox
          >
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowAlottery'"
          :label="$t('改价方案')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-radio-group v-model="eType" :disabled="dataForm[dataForm.editState] == 'N'">
            <a-radio value="1">{{ $t('指定价格') }}</a-radio>
            <a-radio value="2">{{ $t('按比例') }}</a-radio>
          </a-radio-group>
          <div v-if="eType == '1'">
            {{ $t('A票兑换值') }} =
            <a-input
              v-model="dataForm.alottery"
              :disabled="dataForm.allowAlottery == 'N'"
              style="width: 190px"
            ></a-input>
          </div>
          <div v-if="eType == '2'">
            {{ $t('A票兑换值') }} = {{ $t('现有兑换值') }} + {{ $t('现有兑换值') }} ×
            <a-input v-model="dataForm.rate" :disabled="dataForm.allowAlottery == 'N'" style="width: 90px"></a-input>%
          </div>
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowIntegral'"
          :label="$t('积分兑换规则')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-checkbox
            style="color: black; font-weight: 500; font-size: 15px"
            :checked="dataForm[dataForm.editState] == 'Y'"
            @change="(e) => editStatus(e, dataForm.editState)"
            >{{ $t('允许积分兑换') }}</a-checkbox
          >
        </a-form-item>
        <a-form-item
          v-if="dataForm.editState === 'allowIntegral'"
          :label="$t('改价方案')"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 15 }"
        >
          <a-radio-group v-model="eType" :disabled="dataForm[dataForm.editState] == 'N'">
            <a-radio value="1">{{ $t('指定价格') }}</a-radio>
            <a-radio value="2">{{ $t('按比例') }}</a-radio>
          </a-radio-group>
          <div v-if="eType == '1'">
            {{ $t('积分兑换值') }} =
            <a-input
              v-model="dataForm.integral"
              :disabled="dataForm.allowIntegral == 'N'"
              style="width: 190px"
            ></a-input>
          </div>
          <div v-if="eType == '2'">
            {{ $t('积分兑换值') }} = {{ $t('现有兑换值') }} + {{ $t('现有兑换值') }} ×
            <a-input v-model="dataForm.rate" :disabled="dataForm.allowIntegral == 'N'" style="width: 90px"></a-input>%
          </div>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { putAction, getAction } from '@/api/manage'

export default {
  name: 'BatchEditSalesModal',
  data() {
    return {
      title: this.$t('操作'),
      width: 800,
      visible: false,
      model: {},
      goodsIdList: [],
      confirmLoading: false,
      reultList: [],
      dataForm: {
        editState: '',
        saveState: '',
        status: 'N',
      },
      dataForms: {},
      queryList: [],
      treeData: [],
      allChildKind: [],
      paytypeList: [],
      eType: '1',
    }
  },
  watch: {
    'dataForm.editState'(nval) {
      if (nval == 'kind') {
        this.select()
      }
    },
  },
  methods: {
    changeType() {
      this.eType = '1'
    },
    editStatus(e, key) {
      this.$set(this.dataForm, key, e.target.checked ? 'Y' : 'N')
    },
    changeStatus(e) {
      this.$set(this.dataForm, 'status', e.target.checked ? 'Y' : 'N')
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      getAction('/goods/bisGoodsKind/rootList', param)
        .then((res) => {
          if (res.success) {
            that.allChildKind = []
            if (res.result.records.length > 0) {
              res.result.records.forEach((col) => {
                col.key = '' + col.id
                col.value = '' + col.id
                col.title = '' + col.name
                col.id = '' + col.id
                col.pid = '' + col.pid
                col.hasChild = '' + col.hasChild
                if (col.hasChild != '0') {
                  that.getHasChild(col)
                }
              })
              that.treeData = that.getDataByResult(res.result.records)
            } else {
              that.treeData = []
            }
          } else {
            that.$alert(res.message, this.$t('提示'), {
              confirmButtonText: this.$t('确定'),
              callback: (action) => {},
            })
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    getHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.form = '1,7'
      query.pid = record.id
      getAction('/goods/bisGoodsKind/rootList', query)
        .then((res) => {
          if (res.success) {
            if (res.result.records.length > 0) {
              res.result.records.forEach((col) => {
                col.key = '' + col.id
                col.value = '' + col.id
                col.title = '' + col.name
                col.pid = '' + col.pid
                col.id = '' + col.id
                col.hasChild = '' + col.hasChild
                if (col.hasChild != '0') {
                  that.getHasChild(col)
                }
              })
              record.children = that.getDataByResult(res.result.records)
              that.treeData = [...that.treeData]
            }
          } else {
            that.$message.warning(res.message)
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    getDataByResult(result) {
      let that = this
      return result.map((item) => {
        if (item.hasChild == 0 && item.key != '0') {
          that.allChildKind.push(item.key)
        }
        return item
      })
    },
    edit(datas) {
      this.eType = '1'
      this.goodsIdList = datas
      this.dataForm = {
        editState: '',
        saveState: '',
        allowSale: 'N',
        allowCoin: 'N',
        allowLottery: 'N',
        allowPoints: 'N',
        allowAlottery: 'N',
        allowIntegral: 'N',
        status: 'N',
      }
      this.dataForms = {}
      getAction('/pay/bisPayType/list', {
        pageNo: 1,
        pageSize: 9999,
        name: '6,7,10,11,12',
        state: 'Y',
        allowDeviceType: 'EF000000',
        column: 'sort',
        order: 'asc',
      }).then((res) => {
        if (res.success) {
          res.result.records.forEach((r) => {
            if (r.name_dictText.indexOf('payment') > -1) {
              r.name_dictText = r.name_dictText.split('payment')[0]
            } else {
              r.name_dictText = r.name_dictText.split('支付')[0]
            }
          })
          this.paytypeList = res.result.records
        } else {
          this.$message.error(res.message)
        }
      })
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      let that = this
      that.queryList = []
      let url = '/goods/bisGoodsInfor/updateBatch'
      if (that.dataForm.editState == '') {
        that.$message.warn(that.$t('请选择需要修改的属性'))
        return
      } else {
        if (
          that.dataForm.editState != 'barCode' &&
          that.dataForm.editState != 'recyclingList' &&
          that.dataForm.editState != 'allowSale' &&
          that.dataForm.editState != 'allowCoin' &&
          that.dataForm.editState != 'allowLottery' &&
          that.dataForm.editState != 'allowIntegral' &&
          that.dataForm.editState != 'allowPoints' &&
          that.dataForm.editState != 'allowAlottery' &&
          that.dataForm.editState != 'cost' &&
          that.dataForm.saveState == ''
        ) {
          that.$message.warn(that.$t('请选择修改属性'))
          return
        }
        if (that.dataForm.editState == 'state') {
          that.dataForms.state = that.dataForm.saveState
        }
        if (that.dataForm.editState == 'allowMemberDiscount') {
          that.dataForms.allowMemberDiscount = that.dataForm.saveState
        }
        if (that.dataForm.editState == 'kind') {
          that.dataForms.kind = that.dataForm.saveState
        }
        if (that.eType == '2') {
          if (that.dataForm.rate === undefined || that.dataForm.rate === '') {
            if (
              (that.dataForm.editState == 'allowSale' && that.dataForm.allowSale == 'Y') ||
              (that.dataForm.editState == 'allowCoin' && that.dataForm.allowCoin == 'Y') ||
              (that.dataForm.editState == 'allowLottery' && that.dataForm.allowLottery == 'Y') ||
              (that.dataForm.editState == 'allowIntegral' && that.dataForm.allowIntegral == 'Y') ||
              (that.dataForm.editState == 'allowPoints' && that.dataForm.allowPoints == 'Y') ||
              (that.dataForm.editState == 'allowAlottery' && that.dataForm.allowAlottery == 'Y') ||
              that.dataForm.editState == 'cost'
            ) {
              that.$message.warn(that.$t('比例不能为空'))
              return
            }
          }
          if(that.dataForm.editState != 'cost'){
            that.dataForms[that.dataForm.editState] = that.dataForm[that.dataForm.editState]
          }
          that.dataForms.rate = that.dataForm.rate
        }
        if (that.eType == '1') {
          if (that.dataForm.editState == 'allowSale') {
            if (that.dataForm.allowSale == 'Y' && (that.dataForm.price === undefined || that.dataForm.price === '')) {
              that.$message.warn(that.$t('售价不能为空'))
              return
            }
            that.dataForms.allowSale = that.dataForm.allowSale
            that.dataForms.price = that.dataForm.price
          }
          if (that.dataForm.editState == 'allowCoin') {
            if (that.dataForm.allowCoin == 'Y' && (that.dataForm.coin === undefined || that.dataForm.coin === '')) {
              that.$message.warn(that.$t('代币值不能为空'))
              return
            }
            that.dataForms.allowCoin = that.dataForm.allowCoin
            that.dataForms.coin = that.dataForm.coin
          }
          if (that.dataForm.editState == 'allowLottery') {
            if (that.dataForm.allowLottery == 'Y' && (that.dataForm.lottery === undefined || that.dataForm.lottery === '')) {
              that.$message.warn(that.$t('彩票值不能为空'))
              return
            }
            that.dataForms.allowLottery = that.dataForm.allowLottery
            that.dataForms.lottery = that.dataForm.lottery
          }
          if (that.dataForm.editState == 'allowIntegral') {
            if (that.dataForm.allowIntegral == 'Y' && (that.dataForm.integral === undefined || that.dataForm.integral === '')) {
              that.$message.warn(that.$t('积分不能为空'))
              return
            }
            that.dataForms.allowIntegral = that.dataForm.allowIntegral
            that.dataForms.integral = that.dataForm.integral
          }
          if (that.dataForm.editState == 'allowPoints') {
            if (that.dataForm.allowPoints == 'Y' && (that.dataForm.points === undefined || that.dataForm.points === '')) {
              that.$message.warn(that.$t('A币不能为空'))
              return
            }
            that.dataForms.allowPoints = that.dataForm.allowPoints
            that.dataForms.points = that.dataForm.points
          }
          if (that.dataForm.editState == 'allowAlottery') {
            if (that.dataForm.allowAlottery == 'Y' && (that.dataForm.alottery === undefined || that.dataForm.alottery === '')) {
              that.$message.warn(that.$t('A票值不能为空'))
              return
            }
            that.dataForms.allowAlottery = that.dataForm.allowAlottery
            that.dataForms.alottery = that.dataForm.alottery
          }
        }

        if (that.dataForm.editState == 'barCode') {
          url = '/goods/bisGoodsInfor/barCodeBatch'
        }
        if (that.dataForm.editState == 'recyclingList') {
          that.dataForms.recyclingList = [
            {
              status: that.dataForm.status,
              recyclingValue: that.dataForm.recyclingValue,
              recyclingType: that.dataForm.recyclingType,
            },
          ]
        }
      }
      console.log(that.dataForms)
      that.goodsIdList.forEach((item) => {
        let aa = {}
        aa = that.dataForms
        aa = Object.assign({}, that.dataForms)
        aa.id = item
        that.queryList.push(aa)
      })
      putAction(url, that.queryList).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.visible = false
          that.$emit('ok')
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style>
.batchForm .ant-form-item-label {
  width: 30%;
}
.batchForm .ant-form-item-control-wrapper {
  width: 62.5%;
}
</style>